package com.lyl.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.lyl.cloud.common.base.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ZkController {

    public static final String PROVIDER_SERVICE = "http://cloud-zk-provider-service";
    @Value("${server.port}")
    private String serverPort;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/zkTest")
    public Result zkTest(){
        String str = DateUtil.now() + "  zkTest " + serverPort;
        System.out.println("str = " + str);
        return Result.success(str);
    }

    @GetMapping("/zk")
    public Result zk(){
        ResponseEntity<Result> response = restTemplate.getForEntity(PROVIDER_SERVICE + "/zk", Result.class);
        if (response.getStatusCodeValue() == 200){
            System.out.println("zk请求成功 : " + DateUtil.now());
            return response.getBody();
        }
        System.out.println("zk请求成功 : " + DateUtil.now() + " , 错误码：" + response.getStatusCodeValue());
        return Result.fail();
    }
}
